<template>
  <div class="master-list">
      <s-master-list v-if="showSkeleton"></s-master-list>
      <ul v-else v-infinite-scroll="requestData" infinite-scroll-disabled="cellLoading" infinite-scroll-distance="0" infinite-scroll-immediate-check="false">
          <li v-for="(item, index) in masterData">
              <div class="left">
                <my-image class="my-img fade" :imgUrl="item.imgUrl"></my-image>
              </div>
              <div class="content">
                  <div class="top">
                    <p class="content-item">姓名：{{item.name}}</p>
                    <p class="content-item need-margin-top">职务：{{item.job}}</p>
                    <p class="content-item need-margin-top">经验：{{item.exp}}年</p>
                  </div>
                  <div class="bottom">
                    <p class="content-item section">介绍：</p>
                    <p class="content-item dec">{{item.dec}}</p>
                  </div>
              </div>
          </li>
      </ul>
  </div>
</template>

<script>
import { mapState } from "vuex";
import sMasterList from "../../skeletons/sMasterList.vue";
import vCard from "../hotProject/card.vue";
import myImage from "../../lazyloadImg/lazyImage.vue";
import Vue from "vue";
import { InfiniteScroll } from "mint-ui";
Vue.use(InfiniteScroll);

export default {
  name: "master-list",
  data() {
    return {
      masterData: [],
      jobConfig: null,
      cellLoading: true,
      curPage: 0,
      pageNum: 10,
      existIdArr: [],
      isLoadDb: false,
      showSkeleton:true,
      clearId:-1000
    };
  },
  components: { vCard, myImage,sMasterList },
  activated() {},
  mounted() {
    this.clearId=setTimeout(()=>{
      clearTimeout(this.clearId);
      this.clearId=-1000;
      if(this.isLoadDb) this.showSkeleton=false;
    },300);
    this.requestData(false);
  },
  methods: {
    requestData(infinite = true) {
      this.$store.dispatch("toggleLoading", true);
      this.$http
        .post(this.path + "store/Beautician/Beauticianlist", {
          page: this.curPage,
          storeid: this.storeid
        })
        .then(response => {
          this.$store.dispatch("toggleLoading", false);
          if (infinite && this.page == 0) return;
          if (infinite && this.cellLoading) return;
          this.cellLoading = true;
          if (!this.isLoadDb){
            this.isLoadDb=true;
            //如果过了400ms，移除骨架页面
            if(this.clearId==-1000) this.showSkeleton=false;
          }
          if (!this.jobConfig) this.initJobConfig(response.body.result.role);
          let serverData = response.body.result.barr;
          if (!serverData || serverData.length == 0) {
            //数据为空
            this.cellLoading = true;
            if (this.curPage == 0) this.showEmpty = true;
          } else {
            //有数据
            if (serverData.length % 10 == 0) {
              //满一页数据
              this.curPage++;
              this.cellLoading = false;
            } else {
              //不满一页
              this.cellLoading = true;
            }
            let detailsArr = this.analysisData(serverData);
            for (let i = 0; i < detailsArr.length; i++) {
              let orderid = detailsArr[i].id;
              if (this.existIdArr.indexOf(orderid) >= 0) continue;
              this.masterData.push(detailsArr[i]);
              this.existIdArr.push(orderid);
            }
          }
        })
        .catch(response => {
          console.log(response);
          this.$store.dispatch("toggleLoading", false);
        });
    },
    analysisData(data) {
      let detailsArr = [];
      data.forEach((val, index, arr) => {
        detailsArr.push({
          id: val.beautician_id,
          imgUrl: this.soursePath + val.icon,
          name: val.name,
          job: this.jobConfig[val.role],
          dec: val.message,
          exp: val.exp
        });
      });
      return detailsArr;
    },
    initJobConfig(originalData) {
      this.jobConfig = {};
      originalData.forEach((val, index, arr) => {
        this.jobConfig[val.id] = val.rolename;
      });
    }
  },
  computed: {
    ...mapState(["openid", "path", "userid", "storeid",'soursePath'])
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.master-list {
  width: 100%;
  flex: 1;
  padding-bottom: 0.41rem;
  background-color: #efeff0;

  ul {
    padding: 0.1rem 0.09rem 0 0.09rem;

    li {
      width: 100%;
      height: 1.5rem;
      margin-bottom: 0.09rem;
      background-color: #ffffff;
      box-sizing: border-box;
      display: flex;

      .left {
        flex: 1;
        padding: 0.06rem;
        box-sizing: border-box;

        .my-img {
          width: 100%;
          height: 100%;
        }
      }

      .content {
        flex: 1;
        padding: 0.06rem;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;

        .top {
          flex: 1;
        }

        .bottom {
          flex: 1;
        }

        .content-item {
          font-size: 0.13rem;
          color: #595656;
          letter-spacing: 0.01rem;

          &.need-margin-top {
            margin-top: 0.04rem;
          }

          &.dec {
            margin-top: 0.04rem;
            line-height: 0.15rem;
            letter-spacing: 0.01rem;
          }
        }
      }
    }
  }
}
</style>
